package com.andbase.library.app.base;


import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;

import com.andbase.library.R;
import com.andbase.library.app.global.AbActivityManager;
import com.andbase.library.okhttp.AbOkHttpManager;
import com.andbase.library.utils.AbAppUtil;
import com.andbase.library.utils.AbColorUtil;
import com.andbase.library.utils.AbLogUtil;
import com.andbase.library.utils.AbSharedUtil;
import com.andbase.library.utils.AbViewUtil;

/**
 * Copyright upu173.com
 * Author 还如一梦中
 * Date 2016/6/16 13:27
 * Email 396196516@qq.com
 * Info 所有Activity要继承这个父类，便于统一管理
 * 自动加载SharedPreferences中的key为"themeId"的主题
 */
public abstract class AbBaseActivity extends AppCompatActivity {

	/** 主题的key. */
	public static final String THEME_ID = "themeId";

    /** 主题*/
	private int themeId = -1;

    /** 网络请求 */
	public AbOkHttpManager httpManager = null;

	/** 主色调 */
	public int colorPrimary;

	/** Can not perform this action after onSaveInstanceState */
	public boolean saveInstanceState;

    /** 类名 */
	public String pageClassName = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    	//有设置主题
        themeId = AbSharedUtil.getInt(this, THEME_ID,-1);

        if(themeId!=-1){
            this.setTheme(themeId);
        }

		super.onCreate(savedInstanceState);

        pageClassName = this.getClass().getSimpleName();

		AbLogUtil.i(this,"------------------onCreate-----------------------");

        //主色调
		this.colorPrimary = AbColorUtil.getAttrColor(this,R.attr.colorPrimary);

		httpManager = new AbOkHttpManager(this,AbOkHttpManager.CACHAE_TYPE_OPTIMIZE);

		AbActivityManager.getInstance().addActivity(this);

		//默认状态栏透明
		AbAppUtil.setWindowStatusBarTransparent(this,true);

    }

	/**
	 * 万能屏幕适配的setContentView方法
	 * @param layoutResID  布局ID
	 */
	public void setAbContentView(@LayoutRes int layoutResID) {
		View contentView = View.inflate(this,layoutResID,null);
		AbViewUtil.scaleContentView(contentView);
		setContentView(contentView);
	}

	/**
	 * Toolbar设置
	 * @param toolbar
	 * @param title
	 * @param showBack
	 */
	public void initToolbar(Toolbar toolbar, String title, boolean showBack){
		setSupportActionBar(toolbar);
		getSupportActionBar().setDisplayHomeAsUpEnabled(showBack);
		getSupportActionBar().setTitle(title);
	}

	/**
	 * Toolbar title设置
	 * @param title
	 */
	public void setToolbarTitle(String title){
		getSupportActionBar().setTitle(title);
	}
	
	/**
	 * 设置主题ID
	 * @param themeId
	 */
    public void setAppTheme(int themeId){
		this.themeId = themeId;
        this.recreate();  
    }

	/**
	 * 返回默认
	 * @param view
	 */
	public void back(View view){
		finish();
	}

	/**
	 * 拦截返回键
	 */
	@Override
	public void onBackPressed() {
		back(null);
	}

	/**
	 * 拦截返回键
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 是否触发按键为back键
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			onBackPressed();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		saveInstanceState = true;
		//触发home之后activity销毁问题
        AbLogUtil.i(this,"------------------onSaveInstanceState-----------------------");
		outState.putString("Activity", pageClassName);
		super.onSaveInstanceState(outState);
	}


	@Override
	protected void onStart() {
		saveInstanceState = false;
        AbLogUtil.i(this,"------------------onStart-----------------------");
        super.onStart();
    }

    @Override
	protected void onResume() {
		saveInstanceState = false;
        AbLogUtil.i(this,"------------------onResume-----------------------");
        super.onResume();
    }

    @Override
	protected void onPause() {
        AbLogUtil.i(this,"------------------onResume-----------------------");
        super.onPause();
    }



    @Override
	protected void onStop() {
		saveInstanceState = true;
        AbLogUtil.i(this,"------------------onStop-----------------------");
        AbLogUtil.prepareLog(this);
        super.onStop();
    }

    @Override
    public void onLowMemory() {
        AbLogUtil.i(this,"------------------onLowMemory-----------------------");
        super.onLowMemory();
    }


    @Override
	protected void onDestroy() {
        AbLogUtil.i(this,"------------------onDestroy-----------------------");
        AbLogUtil.i(this,"onDestroy",true);
        super.onDestroy();
    }

    /**
	 * 结束
	 */
	@Override
	public void finish() {
		AbLogUtil.i(this,"------------------finish-----------------------");
		AbActivityManager.getInstance().onlyRemoveActivity(this);
		if(httpManager!=null){
			httpManager.cancelAll();
			httpManager = null;
		}

		super.finish();
	}

	/**
	 * 显示空页面
	 * @param layoutResID  布局ID
	 */
	public View showEmptyView(@LayoutRes int layoutResID) {
		View view = View.inflate(this,layoutResID,null);
		ViewGroup contentView = (ViewGroup)findViewById(R.id.ab_content_view);
		ViewGroup emptyView = (ViewGroup)findViewById(R.id.ab_empty_view);
		emptyView.removeAllViews();
		emptyView.addView(view,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
		contentView.setVisibility(View.GONE);
		emptyView.setVisibility(View.VISIBLE);
		return emptyView;
	}

	/**
	 * 隐藏空页面
	 */
	public void hideEmptyView() {
		ViewGroup contentView = (ViewGroup)findViewById(R.id.ab_content_view);
		ViewGroup emptyView = (ViewGroup)findViewById(R.id.ab_empty_view);
		emptyView.setVisibility(View.GONE);
		contentView.setVisibility(View.VISIBLE);
	}
}

